package com.chaowen.yixin;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Switch;
import android.widget.Toast;

import com.chaowen.config.Config;
import com.chaowen.model.UserInfo;
import com.chaowen.util.Calculate;
import com.chaowen.util.SignClient;

public class RightSlidingMenuFragment extends Fragment implements OnItemClickListener,OnCheckedChangeListener{
	//控件
	public static ListView listView;
	private static SimpleAdapter adapter;
	public static Switch openWifi;
	//属性
	private static Context context;
	private Handler handler;
	private SignClient receiver;

	public RightSlidingMenuFragment(Handler handler) {
		this.handler = handler;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = getActivity();
		WifiManager manager = (WifiManager) context
				.getSystemService(Context.WIFI_SERVICE);
		receiver = new SignClient(manager, handler);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.main_right_fragment, container,
				false);
		initViews(view);
		notifyDataSetChanged();
		return view;
	}

	protected void initViews(View view) {
		listView = (ListView) view.findViewById(R.id.myListView);
		openWifi = (Switch) view.findViewById(R.id.openWifi);
		listView.setOnItemClickListener(this);
		openWifi.setOnCheckedChangeListener(this);
	}

	public static void notifyDataSetChanged() {
		if (adapter == null) {
			System.out.println("得到数据:  " + Calculate.getData());
			adapter = new SimpleAdapter(context, Calculate.getData(),
					R.layout.wifilist, new String[] { "info" },
					new int[] { R.id.info });

			int totalHeight = 0;
			for (int i = 0, len = adapter.getCount(); i < len; i++) {
				// listAdapter.getCount()返回数据项的数目
				View listItem = adapter.getView(i, null, listView);
				// 计算子项View 的宽高
				listItem.measure(0, 0);
				// 统计所有子项的总高度
				totalHeight += listItem.getMeasuredHeight();
			}
			ViewGroup.LayoutParams params = listView.getLayoutParams();
			params.height = totalHeight
					+ (listView.getDividerHeight() * (adapter.getCount() - 1));
			// listView.getDividerHeight()获取子项间分隔符占用的高度
			// params.height最后得到整个ListView完整显示需要的高度
			listView.setLayoutParams(params);

			listView.setAdapter(adapter);
			adapter = null;
		} else {
			adapter.notifyDataSetChanged();
		}
	}

	private void switchFragment(Fragment fragment) {
		if (getActivity() == null)
			return;
		MainActivity ra = (MainActivity) getActivity();
		ra.switchContent(fragment);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		String[] msgs = Calculate.getFromList(position);
		Config.className = msgs[1];
		Config.ipAddress = msgs[2];
		Config.socketPort = Integer.parseInt(msgs[3]);
		System.out.println("className: "+Config.className+"   ipAddress: "+Config.ipAddress+"      "+Config.socketPort);
		Toast.makeText(context, "连接上"+Config.className+"的WIFI", 1).show();
		openWifi.setChecked(false);
		
		Fragment_login newContent = new Fragment_login(handler);
		switchFragment(newContent);
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		System.out.println("onCheckedChanged: "+isChecked);
		if(isChecked) {
			Calculate.list.clear();
			Calculate.dateList.clear();
			notifyDataSetChanged();
			UserInfo.isLogout = false;
			Thread thread = new Thread(receiver);
			thread.start();
		}
//		else {
//			listView.clearTextFilter();
//			if(UserInfo.isLogin) {
//				listView.removeAllViewsInLayout();
//				Calculate.list.clear();
//				Calculate.dateList.clear();
//				notifyDataSetChanged();
//			}
//		}
		
	}
}
